package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.acticle.domain.CityScenicDetail;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.query.ScenicCommentQueryObject;
import cn.wolfcode.luowowo.comment.repository.IScenicRepository;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class ScenicCommentServiceImpl implements IScenicCommentService {
    @Autowired
    private MongoTemplate mongoTemplate; //处理比较复杂的操作
    @Autowired
    private IScenicRepository scenicRepository; //处理简单的crud


    public void saveOrUpdate(ScenicComment scenicComment) {
        //createTime
        if(scenicComment.getId() == null){
            scenicComment.setCreateTime(new Date());
        }
        /*
            //编辑操作
            //thumbupnum
            //thumbuplist
        */
        scenicRepository.save(scenicComment);
    }



    public Page<ScenicComment> query(ScenicCommentQueryObject qo, CityScenicDetail detail) {
        //查询封装对象
        Query query = new Query();
        System.out.println(detail.getId());

        //查询条件
        Criteria criteria = null;
        if(detail.getId() != -1){
            criteria = Criteria.where("detailId").is(detail.getId());
        }else {
            criteria = Criteria.where("detailId").ne(qo.getDetailId());
        }
        //添加查询条件
        query.addCriteria(criteria);
        //查询条数
        long count = mongoTemplate.count(query, ScenicComment.class);
        //优化
        if(count == 0){
            return Page.empty();
        }

        //添加排序条件
        query.with(new Sort(Sort.Direction.DESC, "createTime"));
        //添加分页条件
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());
        query.with(pageable);

        //查询评论数据
        List<ScenicComment> list = mongoTemplate.find(query, ScenicComment.class);

        return new PageImpl<>(list, pageable, count);

    }








    public boolean thumbUp(String toid, Long fromid) {
        Optional<ScenicComment> optional = scenicRepository.findById(toid);
        boolean flag = false;
        if(optional.isPresent()){ //对象中有值时
            ScenicComment scenicComment = optional.get();
            List<Long> list = scenicComment.getThumbuplist();
            if(list.contains(fromid)){ //取消点赞
                list.remove(fromid);
               scenicComment.setThumbupnum(scenicComment.getThumbupnum() - 1);
            }else{ //点赞
                list.add(fromid);
                scenicComment.setThumbupnum(scenicComment.getThumbupnum() + 1);
                flag = true;
            }
            saveOrUpdate(scenicComment); //更新数据
        }
        return flag;
    }
}
